#pragma once

#include <iostream>
#include <string>
#include <cstdio>
#include <sys/types.h>
#include <sys/socket.h>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include "log.hpp"
#include <unistd.h>
#include <functional>
using namespace std;
void serverIO(int sock)
{
    char buffer[1024];
    while (true)
    {
        ssize_t n = read(sock, buffer, sizeof(buffer) - 1);
        if (n > 0)
        {
            // 目前我们把读到的数据当成字符串
            buffer[n] = 0;
            cout << "recv message: " << buffer << endl;
            string outbuffer = buffer;
            outbuffer += " server[echo]";
            write(sock, outbuffer.c_str(), outbuffer.size());
        }
        else if (n == 0)
        {
            // 代表客户端client退出
            logMessage(NORMAL, "client quit,me too!");
            break;
        }
    }
    close(sock);
    
}
class Task
{

    using func_t = std::function<void(int)>;
    // typedef std::function<int(int,int)> func_t;
public:
    Task()
    {
    }
    Task(int sock, func_t func)
        : _sock(sock), _callback(func)
    {}
    void operator()()
    {
        _callback(_sock);
    }

private:
    int _sock;
    func_t _callback;
};
